package clothes.tools.dao.impl;
import clothes.tools.dao.CustomerDao;
import clothes.tools.pojo.Customer;

import java.util.List;

public class CustomerDaoImpl extends BaseDao implements CustomerDao {
    @Override
    public Customer queryByUsername(String username) {
        String sql="select cid,mima,gender,balance,e_mile from customer where cid=?" ;
        return queryForOne(sql,Customer.class,username);
    }

    @Override
    public Customer queryByUsernameAndPassword(String username, String password) {
        String sql="select cid,mima,gender,balance,e_mile from customer where cid=? and mima=?" ;
        return queryForOne(sql,Customer.class,username,password);
    }

    @Override
    public int saveUser(Customer user) {
        String sql="insert into customer(cid,mima,gender,balance,e_mile) values(?,?,?,?,?)";
        return upDate(sql,user.getCid(),user.getMima(),user.getGender(),user.getBalance(),user.getE_mile());
    }

    @Override
    public int deleteById(String id) {
        String sql="delete from customer where cid=?";
        return upDate(sql,id);
    }

    @Override
    public int updateCustomer(Customer customer) {
        String sql="update customer set mima=?,e_mile=?,gender=? where cid=?";
        return upDate(sql,customer.getMima(),customer.getE_mile(),customer.getGender(),customer.getCid());
    }

    @Override
    public List<Customer> queryCustomer() {
        String sql="select * from customer";
        return queryForList(sql,Customer.class);
    }

    @Override
    public List<Customer> queryCustomer(String page, String limit) {
        String sql="select * from customer limit ?,?";
        int page1=Integer.parseInt(page);
        int limit1=Integer.parseInt(limit);
        return queryForList(sql, Customer.class,(page1-1)*limit1,limit1);
    }

    @Override
    public List<Customer> queryCustomers(String page, String limit, String name) {
        String sql="select * from customer where cid like '%"+name+"%' limit ?,?";
        int page1=Integer.parseInt(page);
        int limit1=Integer.parseInt(limit);
        return queryForList(sql, Customer.class,(page1-1)*limit1,limit1);
    }
}
